<?php
/**
 * @link http://framework.zend.com/manual/en/zend.form.elements.html
 */
class Areal_Validate_DateLessOrEqualThan extends Zend_Validate_Abstract
{
    const NOT_MATCH = 'notMatch';

    protected $_messageTemplates = array(
        self::NOT_MATCH => 'Start date should be less than end date'
    );

    protected $_end_key = null;

    public function __construct($end_key)
    {
        $this->_end_key = $end_key;
    }

    public function isValid($value, $context = null)
    {
        $value = (string) $value;
        $this->_setValue($value);

        $start = strtotime($value);
        if ( ! isset($context[$this->_end_key])) $context[$this->_end_key] = null;
        $end = strtotime($context[$this->_end_key]);

        if ( ! $start)
        {
            return true;
        }
        if ( ! $end)
        {
            return true;
        }
        if ( $start <= $end )
        {
            return true;
        }

        $this->_error(self::NOT_MATCH);
        return false;
    }
}